import 'package:flutter/foundation.dart';

/// 活动状态枚举
enum ActivityStatus {
  upcoming,    // 即将开始
  ongoing,     // 进行中
  completed,   // 已结束
  cancelled,   // 已取消
}

/// 活动类型枚举
enum ActivityType {
  exhibition,  // 展览
  workshop,    // 工作坊
  lecture,     // 讲座
  performance, // 表演
  competition, // 比赛
  charity,     // 慈善
}

/// 活动模型
class Activity {
  final String id;
  final String title;
  final String description;
  final DateTime startTime;
  final DateTime endTime;
  final String location;
  final String category;
  final ActivityStatus status;
  final ActivityType type;
  final int maxParticipants;
  final int currentParticipants;
  final double price;
  final List<String> images;
  final String organizerId;
  final String organizerName;
  final Map<String, dynamic> metadata;
  final DateTime createdAt;
  final DateTime updatedAt;
  final bool isHot;
  final bool isLatest;
  final bool isFeatured;
  final double rating;
  final int reviewCount;
  final List<String> tags;

  const Activity({
    required this.id,
    required this.title,
    required this.description,
    required this.startTime,
    required this.endTime,
    required this.location,
    required this.category,
    required this.status,
    required this.type,
    this.maxParticipants = 0,
    this.currentParticipants = 0,
    this.price = 0.0,
    this.images = const [],
    required this.organizerId,
    required this.organizerName,
    this.metadata = const {},
    required this.createdAt,
    required this.updatedAt,
    this.isHot = false,
    this.isLatest = false,
    this.isFeatured = false,
    this.rating = 0.0,
    this.reviewCount = 0,
    this.tags = const [],
  });

  /// 从JSON创建Activity对象
  factory Activity.fromJson(Map<String, dynamic> json) {
    return Activity(
      id: json['id'] ?? '',
      title: json['title'] ?? '',
      description: json['description'] ?? '',
      startTime: DateTime.parse(json['start_time'] ?? DateTime.now().toIso8601String()),
      endTime: DateTime.parse(json['end_time'] ?? DateTime.now().toIso8601String()),
      location: json['location'] ?? '',
      category: json['category'] ?? '',
      status: ActivityStatus.values.firstWhere(
        (e) => e.name == json['status'],
        orElse: () => ActivityStatus.upcoming,
      ),
      type: ActivityType.values.firstWhere(
        (e) => e.name == json['type'],
        orElse: () => ActivityType.exhibition,
      ),
      maxParticipants: json['max_participants'] ?? 0,
      currentParticipants: json['current_participants'] ?? 0,
      price: (json['price'] ?? 0.0).toDouble(),
      images: List<String>.from(json['images'] ?? []),
      organizerId: json['organizer_id'] ?? '',
      organizerName: json['organizer_name'] ?? '',
      metadata: Map<String, dynamic>.from(json['metadata'] ?? {}),
      createdAt: DateTime.parse(json['created_at'] ?? DateTime.now().toIso8601String()),
      updatedAt: DateTime.parse(json['updated_at'] ?? DateTime.now().toIso8601String()),
      isHot: json['is_hot'] ?? false,
      isLatest: json['is_latest'] ?? false,
      isFeatured: json['is_featured'] ?? false,
      rating: (json['rating'] ?? 0.0).toDouble(),
      reviewCount: json['review_count'] ?? 0,
      tags: List<String>.from(json['tags'] ?? []),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'start_time': startTime.toIso8601String(),
      'end_time': endTime.toIso8601String(),
      'location': location,
      'category': category,
      'status': status.name,
      'type': type.name,
      'max_participants': maxParticipants,
      'current_participants': currentParticipants,
      'price': price,
      'images': images,
      'organizer_id': organizerId,
      'organizer_name': organizerName,
      'metadata': metadata,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
      'is_hot': isHot,
      'is_latest': isLatest,
      'is_featured': isFeatured,
      'rating': rating,
      'review_count': reviewCount,
      'tags': tags,
    };
  }

  /// 复制并修改部分属性
  Activity copyWith({
    String? id,
    String? title,
    String? description,
    DateTime? startTime,
    DateTime? endTime,
    String? location,
    String? category,
    ActivityStatus? status,
    ActivityType? type,
    int? maxParticipants,
    int? currentParticipants,
    double? price,
    List<String>? images,
    String? organizerId,
    String? organizerName,
    Map<String, dynamic>? metadata,
    DateTime? createdAt,
    DateTime? updatedAt,
    bool? isHot,
    bool? isLatest,
    bool? isFeatured,
    double? rating,
    int? reviewCount,
    List<String>? tags,
  }) {
    return Activity(
      id: id ?? this.id,
      title: title ?? this.title,
      description: description ?? this.description,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
      location: location ?? this.location,
      category: category ?? this.category,
      status: status ?? this.status,
      type: type ?? this.type,
      maxParticipants: maxParticipants ?? this.maxParticipants,
      currentParticipants: currentParticipants ?? this.currentParticipants,
      price: price ?? this.price,
      images: images ?? this.images,
      organizerId: organizerId ?? this.organizerId,
      organizerName: organizerName ?? this.organizerName,
      metadata: metadata ?? this.metadata,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      isHot: isHot ?? this.isHot,
      isLatest: isLatest ?? this.isLatest,
      isFeatured: isFeatured ?? this.isFeatured,
      rating: rating ?? this.rating,
      reviewCount: reviewCount ?? this.reviewCount,
      tags: tags ?? this.tags,
    );
  }

  /// 获取活动状态显示文本
  String get statusText {
    switch (status) {
      case ActivityStatus.upcoming:
        return '即将开始';
      case ActivityStatus.ongoing:
        return '进行中';
      case ActivityStatus.completed:
        return '已结束';
      case ActivityStatus.cancelled:
        return '已取消';
    }
  }

  /// 获取活动类型显示文本
  String get typeText {
    switch (type) {
      case ActivityType.exhibition:
        return '展览';
      case ActivityType.workshop:
        return '工作坊';
      case ActivityType.lecture:
        return '讲座';
      case ActivityType.performance:
        return '表演';
      case ActivityType.competition:
        return '比赛';
      case ActivityType.charity:
        return '慈善';
    }
  }

  /// 检查是否可以报名
  bool get canRegister {
    return status == ActivityStatus.upcoming && 
           currentParticipants < maxParticipants;
  }

  /// 获取剩余名额
  int get remainingSlots {
    return maxParticipants - currentParticipants;
  }

  /// 检查是否已满员
  bool get isFull {
    return currentParticipants >= maxParticipants;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is Activity && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return 'Activity{id: $id, title: $title, status: $status, type: $type}';
  }
}

/// 活动筛选器
enum ActivityFilter {
  all,        // 全部
  upcoming,   // 即将开始
  ongoing,    // 进行中
  completed,  // 已结束
  hot,        // 热门
  latest,     // 最新
  featured,   // 精选
}

/// 活动筛选器扩展
extension ActivityFilterExtension on ActivityFilter {
  String get displayName {
    switch (this) {
      case ActivityFilter.all:
        return '全部';
      case ActivityFilter.upcoming:
        return '即将开始';
      case ActivityFilter.ongoing:
        return '进行中';
      case ActivityFilter.completed:
        return '已结束';
      case ActivityFilter.hot:
        return '热门';
      case ActivityFilter.latest:
        return '最新';
      case ActivityFilter.featured:
        return '精选';
    }
  }
}